<?php

//就近 www.jiujin.me
global $_W, $_GPC;

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
	ca('saoma.list.view');
	$list = pdo_fetchall("SELECT * FROM " . tablename('ewei_shop_saoma') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY displayorder DESC");
	foreach($list as $key => $value){
		$ae = pdo_fetch("SELECT title FROM " . tablename('activity_exchange') . " WHERE id = '{$value['goods']}' and uniacid = '{$_W['uniacid']}'");
		$list[$key]['goods'] = $ae['title']; 
		$ac = pdo_fetch("SELECT title FROM " . tablename('activity_coupon') . " WHERE couponid = '{$value['jifen']}' and uniacid = '{$_W['uniacid']}'");
		$list[$key]['jifen'] = $ac['title'];
		$list[$key]['lianjie'] = 'http://wx.lanxuevip.com/app/index.php?i=5&c=entry&do=saoma&m=ewei_shop&id='.$value['id'];
	}
} elseif ($operation == 'post') {
	$id = intval($_GPC['id']);
	if (empty($id)) {
		ca('saoma.list.add');
	} else {
		ca('saoma.list.edit|saoma.list.view');
	}
	if (checksubmit('submit')) {
		$data = array('uniacid' => $_W['uniacid'], 'displayorder' => intval($_GPC['displayorder']), 'title' => trim($_GPC['title']), 'goods' => trim($_GPC['goods']), 'title1' => trim($_GPC['title1']), 'thumb1' => save_media($_GPC['thumb1']), 'jifen' => trim($_GPC['jifen']), 'title2' => trim($_GPC['title2']), 'thumb2' => save_media($_GPC['thumb2']), 'status' => intval($_GPC['status']), 'createtime' => time());
		if (!empty($id)) {
			pdo_update('ewei_shop_saoma', $data, array('id' => $id));
			plog('saoma.list.edit', "修改扫码领奖 ID: {$id}");
		} else {
			pdo_insert('ewei_shop_saoma', $data);
			$id = pdo_insertid();
			plog('saoma.list.add', "修改扫码领奖 ID: {$id}");
		}
		message('更新扫码领奖成功！', $this->createWebUrl('saoma/list', array('op' => 'display')), 'success');
	}
	$time = time();
	//真实商品
	//$goods = pdo_fetchall("SELECT * FROM " . tablename('activity_exchange') . " WHERE uniacid = '{$_W['uniacid']}' and '{$time}' > starttime and '{$time}' < endtime");
	//代金券
	//$jifen = pdo_fetchall("SELECT * FROM " . tablename('activity_coupon') . " WHERE type = 2 and uniacid = '{$_W['uniacid']}' and '{$time}' > starttime and '{$time}' < endtime");
	$saoma = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_saoma') . " WHERE id = '{$id}' and uniacid = '{$_W['uniacid']}'");
} elseif ($operation == 'delete') {
	ca('saoma.list.delete');
	$id = intval($_GPC['id']);
	$saoma = pdo_fetch("SELECT id,title  FROM " . tablename('ewei_shop_saoma') . " WHERE id = '{$id}' AND uniacid=" . $_W['uniacid'] . "");
	if (empty($saoma)) {
		message('抱歉，扫码领奖不存在或是已经被删除！', $this->createWebUrl('saoma/list', array('op' => 'display')), 'error');
	}
	pdo_delete('ewei_shop_saoma', array('id' => $id));
	plog('saoma.list.delete', "删除扫码领奖 ID: {$id} 标题: {$saoma['title']}");
	message('扫码领奖删除成功！', $this->createWebUrl('saoma/list', array('op' => 'display')), 'success');
}
load()->func('tpl');
include $this->template('web/saoma/list');